using System;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling.Shell;

namespace Microsoft.Modeling.StoryboardDesigner.DslPackage
{
    internal partial class StoryboardDesignerCommandSet
    {
        private static SaveFileDialog saveImageDlg;

        #region Context menu handling

        /// <summary>
        /// Returns a flag indicating whether the "Save image"
        /// context menu should be displayed.
        /// </summary>
        private void OnStatusSaveImage(object sender, EventArgs args)
        {
            MenuCommand command = sender as MenuCommand;

            if(command == null)
            {
                return;
            }

            command.Enabled = true;
        }

        /// <summary>
        /// Context menu handler - called when "Save Image"
        /// is clicked.
        /// </summary>
        private void OnSaveImage(object sender, EventArgs args)
        {
            try
            {
                // Get the current diagram
                StoryboardDiagram storyboardDiagram = this.CurrentDocView.CurrentDiagram as StoryboardDiagram;

                if(storyboardDiagram == null)
                {
                    return;
                }

                // Get the name of the file to use
                string fileName = GetImageFileNameFromUser();

                if(fileName == null)
                {
                    return;
                }

                // Save the file
                SaveDiagramImage(
                    storyboardDiagram, 
                    fileName, 
                    Diagram.CreateBitmapPreference.FavorClarityOverSmallSize);
            }
            catch(Exception ex)
            {
                PackageUtility.ShowError(
                    this.ServiceProvider,
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Properties.Resources.GeneralException,
                        Environment.NewLine,
                        ex.Message));
            }
        }

        #endregion

        #region Save image code
        private static string GetImageFileNameFromUser()
        {
            if(SaveImageDialog.ShowDialog() == DialogResult.Cancel)
            {
                return null;
            }

            return SaveImageDialog.FileName;
        }

        private static SaveFileDialog SaveImageDialog
        {
            get
            {
                if(saveImageDlg == null)
                {
                    saveImageDlg = new SaveFileDialog();
                    saveImageDlg.CheckPathExists = true;
                    saveImageDlg.Filter =
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Properties.Resources.SaveImageDialogFilter);

                    saveImageDlg.OverwritePrompt = true;
                    saveImageDlg.Title =
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Properties.Resources.SaveImageDialogTitle);
                }

                return saveImageDlg;
            }
        }

        /// <summary>
        /// Save the image of the specified diagram
        /// </summary>
        /// <param name="diag"></param>
        /// <param name="fileName"></param>
        /// <param name="preference"></param>
        private static void SaveDiagramImage(
            StoryboardDiagram storyboardDiagram,
            string fileName,
            Diagram.CreateBitmapPreference preference)
        {
            Bitmap bmp = storyboardDiagram.CreateBitmap(
                            storyboardDiagram.NestedChildShapes, preference);

            // NB only ever saving as a JPEG - not giving the user any choice
            bmp.Save(fileName, ImageFormat.Jpeg);
        }
        #endregion
    }
}